<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Cnode extends controller {
	//获取流程设置节点列表
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view, 'admin/workflow/id');
		$where  = array();
		$roleid = (int)input::get_post('roleid');
		if (is_numeric($roleid) && $roleid) {
			$where[] = array('roleid', $roleid);
		}
		$where[]= array('workflowid', (int)input::get_post('workflowid'));
		$name   = input::post('name');
		if(!empty($name)) {
			$where[]= array('name', '%'.$name.'%', \classlib\orm::OP_LIKE);
		}
		$rorm = C\model::load('main.sys.role');
		$form = C\model::load('main.proc.nodeform');
		$norm = C\model::load('main.proc.node');
		$list = $norm->get_list(0, -1, $where, '*', 'id', \classlib\orm::ASC);
		foreach ($list as &$item) {
			$data = $rorm->get_one($item['roleid']);
			$item['role'] = $data['name'];
			$data = $form->get_one($item['formid']);
			$item['form'] = $data['name'];
		}
		$view->assign('total', count($list));
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	/**
	 * @desc    保存节点资料
	 * @doc     保存节点
	 * @access  需要授权
	 */
	public function dosave() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$lang  = C\lang::inst('adm');
		$workflowid = (int)input::post('workflowid');
		$workflow 	= C\model::load('main.proc.workflow')->get_one($workflowid);
		if (empty($workflow)) {
			$view->assign_error('50003', $lang->get('workflow', '50003'));
		}
		$id    = (int)input::get_post('id');
		$data  = array('workflowid'=>$workflowid, 'stime'=>C\config::$nowtime);
		$filter= new \classlib\validator\auditor();
		$filter->lang($lang);
		try {
			$data['name']   = $filter->reset('name')->required()->maxlen(127)->run();
			$data['actions']= $filter->reset('actions')->required()->run();
			$data['roleid'] = $filter->reset('roleid')->numeric()->run();
			$data['preid']  = $filter->reset('preid')->numeric()->run();
			$data['expire'] = $filter->reset('expire')->numeric()->run();
			$data['formid'] = $filter->reset('formid')->numeric()->run();
			$data['result'] = $filter->reset('result')->required()->run();
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		if ($id > 0) {
			C\model::load('main.proc.node')->save($id, $data);
		} else {
			C\model::load('main.proc.node')->new_one($data);
		}
		$view->display();
	}
	
	/**
	 * @desc    删除节点资料
	 * @doc     删除节点
	 * @access  需要授权
	 */
	public function dodelete() {
		$view = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::post('ids');
		$ids  = explode(',', $ids);
		$ids  = array_filter($ids, 'is_numeric');
		$lang = C\lang::inst('adm');
		if(empty($ids)) {
			$view->assign_error('50004', $lang->get('workflow', '50004'));
		}
		$sorm = C\model::load('main.proc.nodestate');
		$norm = C\model::load('main.proc.node');
		foreach ($ids as $id) {
			$exists = $sorm->isexist(array('nodeid'=>$id));
			if ($exists) {
				$view->assign_error('50005', $lang->get('workflow', '50005'));
			}
			$norm->delete($id);
		}
		$view->display();
	}
}